
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_Hans">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Django 便捷函数 &#8212; Django 3.2.11.dev 文档</title>
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/language_data.js"></script>
    <link rel="index" title="索引" href="../../genindex.html" />
    <link rel="search" title="搜索" href="../../search.html" />
    <link rel="next" title="通用视图" href="generic-views.html" />
    <link rel="prev" title="文件上传" href="file-uploads.html" />



 
<script src="../../templatebuiltins.js"></script>
<script>
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Django 3.2.11.dev 文档</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../index.html">Home</a>  |
        <a title="Table of contents" href="../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../genindex.html">Index</a>  |
        <a title="Module index" href="../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="file-uploads.html" title="文件上传">previous</a>
     |
    <a href="../index.html" title="使用 Django" accesskey="U">up</a>
   |
    <a href="generic-views.html" title="通用视图">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="topics-http-shortcuts">
            
  <div class="section" id="s-module-django.shortcuts">
<span id="s-django-shortcut-functions"></span><span id="module-django.shortcuts"></span><span id="django-shortcut-functions"></span><h1>Django 便捷函数<a class="headerlink" href="#module-django.shortcuts" title="永久链接至标题">¶</a></h1>
<p id="index-0">包 <code class="docutils literal notranslate"><span class="pre">django.shortcuts</span></code> 收集助手函数和“跨”多级mvc的类，换句话说，为了方便起见，这些函数/类引入受控耦合。</p>
<div class="section" id="s-render">
<span id="render"></span><h2><code class="docutils literal notranslate"><span class="pre">render()</span></code><a class="headerlink" href="#render" title="永久链接至标题">¶</a></h2>
<dl class="function">
<dt id="django.shortcuts.render">
<code class="descname">render</code>(<em>request</em>, <em>template_name</em>, <em>context=None</em>, <em>content_type=None</em>, <em>status=None</em>, <em>using=None</em>)<a class="headerlink" href="#django.shortcuts.render" title="永久链接至目标">¶</a></dt>
<dd><p>将给定的模板与给定的上下文字典组合在一起，并以渲染的文本返回一个 <a class="reference internal" href="../../ref/request-response.html#django.http.HttpResponse" title="django.http.HttpResponse"><code class="xref py py-class docutils literal notranslate"><span class="pre">HttpResponse</span></code></a> 对象。</p>
<p>Django没有提供返回 <a class="reference internal" href="../../ref/template-response.html#django.template.response.TemplateResponse" title="django.template.response.TemplateResponse"><code class="xref py py-class docutils literal notranslate"><span class="pre">TemplateResponse</span></code></a> 的便捷函数，因为 <a class="reference internal" href="../../ref/template-response.html#django.template.response.TemplateResponse" title="django.template.response.TemplateResponse"><code class="xref py py-class docutils literal notranslate"><span class="pre">TemplateResponse</span></code></a> 的构造函数提供了与 <a class="reference internal" href="#django.shortcuts.render" title="django.shortcuts.render"><code class="xref py py-func docutils literal notranslate"><span class="pre">render()</span></code></a> 同等程度的便利。</p>
</dd></dl>

<div class="section" id="s-required-arguments">
<span id="required-arguments"></span><h3>必选参数<a class="headerlink" href="#required-arguments" title="永久链接至标题">¶</a></h3>
<dl class="docutils">
<dt><code class="docutils literal notranslate"><span class="pre">request</span></code></dt>
<dd>用于生成此响应的请求对象。</dd>
<dt><code class="docutils literal notranslate"><span class="pre">template_name</span></code></dt>
<dd>要使用的模板的全名或模板名称的序列。如果给定一个序列，则将使用存在的第一个模板。有关如何查找模板的更多信息，请参见 <a class="reference internal" href="../templates.html#template-loading"><span class="std std-ref">模板加载文档</span></a> 。</dd>
</dl>
</div>
<div class="section" id="s-optional-arguments">
<span id="optional-arguments"></span><h3>可选参数<a class="headerlink" href="#optional-arguments" title="永久链接至标题">¶</a></h3>
<dl class="docutils">
<dt><code class="docutils literal notranslate"><span class="pre">context</span></code></dt>
<dd>要添加到模板上下文的值的字典。 默认情况下，这是一个空的字典。 如果字典中的值是可调用的，则视图将在渲染模板之前调用它。</dd>
<dt><code class="docutils literal notranslate"><span class="pre">content_type</span></code></dt>
<dd>用于结果文档的 MIME 类型。默认 <code class="docutils literal notranslate"><span class="pre">'text/html'</span></code> 。</dd>
<dt><code class="docutils literal notranslate"><span class="pre">status</span></code></dt>
<dd>响应的状态码默认为 <code class="docutils literal notranslate"><span class="pre">200</span></code>。</dd>
<dt><code class="docutils literal notranslate"><span class="pre">using</span></code></dt>
<dd>用于加载模板的模板引擎的 <a class="reference internal" href="../../ref/settings.html#std:setting-TEMPLATES-NAME"><code class="xref std std-setting docutils literal notranslate"><span class="pre">NAME</span></code></a> 。</dd>
</dl>
</div>
<div class="section" id="s-example">
<span id="example"></span><h3>例如<a class="headerlink" href="#example" title="永久链接至标题">¶</a></h3>
<p>下面的示例使用 MIME 类型呈现模板 <code class="docutils literal notranslate"><span class="pre">myapp/index.html</span></code> <em class="mimetype">application/xhtml+xml</em> ：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.shortcuts</span> <span class="kn">import</span> <span class="n">render</span>

<span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="c1"># View code here...</span>
    <span class="k">return</span> <span class="n">render</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="s1">&#39;myapp/index.html&#39;</span><span class="p">,</span> <span class="p">{</span>
        <span class="s1">&#39;foo&#39;</span><span class="p">:</span> <span class="s1">&#39;bar&#39;</span><span class="p">,</span>
    <span class="p">},</span> <span class="n">content_type</span><span class="o">=</span><span class="s1">&#39;application/xhtml+xml&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>此示例相当于：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.http</span> <span class="kn">import</span> <span class="n">HttpResponse</span>
<span class="kn">from</span> <span class="nn">django.template</span> <span class="kn">import</span> <span class="n">loader</span>

<span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="c1"># View code here...</span>
    <span class="n">t</span> <span class="o">=</span> <span class="n">loader</span><span class="o">.</span><span class="n">get_template</span><span class="p">(</span><span class="s1">&#39;myapp/index.html&#39;</span><span class="p">)</span>
    <span class="n">c</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;foo&#39;</span><span class="p">:</span> <span class="s1">&#39;bar&#39;</span><span class="p">}</span>
    <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="n">t</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">request</span><span class="p">),</span> <span class="n">content_type</span><span class="o">=</span><span class="s1">&#39;application/xhtml+xml&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="s-redirect">
<span id="redirect"></span><h2><code class="docutils literal notranslate"><span class="pre">redirect()</span></code><a class="headerlink" href="#redirect" title="永久链接至标题">¶</a></h2>
<dl class="function">
<dt id="django.shortcuts.redirect">
<code class="descname">redirect</code>(<em>to</em>, <em>*args</em>, <em>permanent=False</em>, <em>**kwargs</em>)<a class="headerlink" href="#django.shortcuts.redirect" title="永久链接至目标">¶</a></dt>
<dd><p>返回一个 <a class="reference internal" href="../../ref/request-response.html#django.http.HttpResponseRedirect" title="django.http.HttpResponseRedirect"><code class="xref py py-class docutils literal notranslate"><span class="pre">HttpResponseRedirect</span></code></a>，指向传递参数的适当 URL。</p>
<p>参数可以是：</p>
<ul class="simple">
<li>一个模型：模型的 <a class="reference internal" href="../../ref/models/instances.html#django.db.models.Model.get_absolute_url" title="django.db.models.Model.get_absolute_url"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_absolute_url()</span></code></a> 函数将被调用。</li>
<li>视图名，可能带有的参数：<a class="reference internal" href="../../ref/urlresolvers.html#django.urls.reverse" title="django.urls.reverse"><code class="xref py py-func docutils literal notranslate"><span class="pre">reverse()</span></code></a> 将被用于反向解析名称。</li>
<li>一个绝对或相对 URL，将按原样用作重定向位置。</li>
</ul>
<p>默认情况下发出临时重定向；通过传递 <code class="docutils literal notranslate"><span class="pre">permanent=True</span></code> 发出永久重定向。</p>
</dd></dl>

<div class="section" id="s-examples">
<span id="examples"></span><h3>示例<a class="headerlink" href="#examples" title="永久链接至标题">¶</a></h3>
<p>你可以通过多种方法使用 <a class="reference internal" href="#django.shortcuts.redirect" title="django.shortcuts.redirect"><code class="xref py py-func docutils literal notranslate"><span class="pre">redirect()</span></code></a> 函数。</p>
<ol class="arabic">
<li><p class="first">传递对象，对象的 <a class="reference internal" href="../../ref/models/instances.html#django.db.models.Model.get_absolute_url" title="django.db.models.Model.get_absolute_url"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_absolute_url()</span></code></a> 方法将被调用来指向重定向地址：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.shortcuts</span> <span class="kn">import</span> <span class="n">redirect</span>

<span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="o">...</span>
    <span class="n">obj</span> <span class="o">=</span> <span class="n">MyModel</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="o">...</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">redirect</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>
</pre></div>
</div>
</li>
<li><p class="first">传递视图名和一些可选的位置或关键字参数；URL 将使用 <a class="reference internal" href="../../ref/urlresolvers.html#django.urls.reverse" title="django.urls.reverse"><code class="xref py py-func docutils literal notranslate"><span class="pre">reverse()</span></code></a> 方法来反向解析：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="o">...</span>
    <span class="k">return</span> <span class="n">redirect</span><span class="p">(</span><span class="s1">&#39;some-view-name&#39;</span><span class="p">,</span> <span class="n">foo</span><span class="o">=</span><span class="s1">&#39;bar&#39;</span><span class="p">)</span>
</pre></div>
</div>
</li>
<li><p class="first">传递硬编码 URL 来重定向：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="o">...</span>
    <span class="k">return</span> <span class="n">redirect</span><span class="p">(</span><span class="s1">&#39;/some/url/&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>这也适用于完整的 URL ：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="o">...</span>
    <span class="k">return</span> <span class="n">redirect</span><span class="p">(</span><span class="s1">&#39;https://example.com/&#39;</span><span class="p">)</span>
</pre></div>
</div>
</li>
</ol>
<p>默认情况下，<a class="reference internal" href="#django.shortcuts.redirect" title="django.shortcuts.redirect"><code class="xref py py-func docutils literal notranslate"><span class="pre">redirect()</span></code></a> 返回临时重定向。所有以上形式都接受 <code class="docutils literal notranslate"><span class="pre">permanent</span></code> 参数；如果设置为 <code class="docutils literal notranslate"><span class="pre">True</span></code> 会返回一个永久重定向：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="o">...</span>
    <span class="n">obj</span> <span class="o">=</span> <span class="n">MyModel</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="o">...</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">redirect</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">permanent</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="s-get-object-or-404">
<span id="get-object-or-404"></span><h2><code class="docutils literal notranslate"><span class="pre">get_object_or_404()</span></code><a class="headerlink" href="#get-object-or-404" title="永久链接至标题">¶</a></h2>
<dl class="function">
<dt id="django.shortcuts.get_object_or_404">
<code class="descname">get_object_or_404</code>(<em>klass</em>, <em>*args</em>, <em>**kwargs</em>)<a class="headerlink" href="#django.shortcuts.get_object_or_404" title="永久链接至目标">¶</a></dt>
<dd><p>在给定的模型管理器( model manager) 上调用 <a class="reference internal" href="../../ref/models/querysets.html#django.db.models.query.QuerySet.get" title="django.db.models.query.QuerySet.get"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get()</span></code></a> ，但它会引发 <a class="reference internal" href="views.html#django.http.Http404" title="django.http.Http404"><code class="xref py py-class docutils literal notranslate"><span class="pre">Http404</span></code></a> 而不是模型的 <a class="reference internal" href="../../ref/models/class.html#django.db.models.Model.DoesNotExist" title="django.db.models.Model.DoesNotExist"><code class="xref py py-class docutils literal notranslate"><span class="pre">DoesNotExist</span></code></a> 异常。</p>
</dd></dl>

<div class="section" id="s-id1">
<span id="id1"></span><h3>必选参数<a class="headerlink" href="#id1" title="永久链接至标题">¶</a></h3>
<dl class="docutils">
<dt><code class="docutils literal notranslate"><span class="pre">klass</span></code></dt>
<dd>从中获取对象的 <a class="reference internal" href="../../ref/models/instances.html#django.db.models.Model" title="django.db.models.Model"><code class="xref py py-class docutils literal notranslate"><span class="pre">Model</span></code></a> 类， <a class="reference internal" href="../db/managers.html#django.db.models.Manager" title="django.db.models.Manager"><code class="xref py py-class docutils literal notranslate"><span class="pre">Manager</span></code></a> ，或  <a class="reference internal" href="../../ref/models/querysets.html#django.db.models.query.QuerySet" title="django.db.models.query.QuerySet"><code class="xref py py-class docutils literal notranslate"><span class="pre">QuerySet</span></code></a> 实例。</dd>
<dt><code class="docutils literal notranslate"><span class="pre">**kwargs</span></code></dt>
<dd>查询参数，应采用 <code class="docutils literal notranslate"><span class="pre">get()</span></code> 和 <code class="docutils literal notranslate"><span class="pre">filter()</span></code> 接受的格式。</dd>
</dl>
</div>
<div class="section" id="s-id2">
<span id="id2"></span><h3>例如<a class="headerlink" href="#id2" title="永久链接至标题">¶</a></h3>
<p>下面的例子是展示从 <code class="docutils literal notranslate"><span class="pre">MyModel</span></code> 中获取主键为1的对象：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.shortcuts</span> <span class="kn">import</span> <span class="n">get_object_or_404</span>

<span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="n">obj</span> <span class="o">=</span> <span class="n">get_object_or_404</span><span class="p">(</span><span class="n">MyModel</span><span class="p">,</span> <span class="n">pk</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
</pre></div>
</div>
<p>此示例相当于：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.http</span> <span class="kn">import</span> <span class="n">Http404</span>

<span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">obj</span> <span class="o">=</span> <span class="n">MyModel</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">pk</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
    <span class="k">except</span> <span class="n">MyModel</span><span class="o">.</span><span class="n">DoesNotExist</span><span class="p">:</span>
        <span class="k">raise</span> <span class="n">Http404</span><span class="p">(</span><span class="s2">&quot;No MyModel matches the given query.&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>如上所示，最常用的使用案例是传递 <a class="reference internal" href="../../ref/models/instances.html#django.db.models.Model" title="django.db.models.Model"><code class="xref py py-class docutils literal notranslate"><span class="pre">Model</span></code></a> 。但是，你也可以传递一个 <a class="reference internal" href="../../ref/models/querysets.html#django.db.models.query.QuerySet" title="django.db.models.query.QuerySet"><code class="xref py py-class docutils literal notranslate"><span class="pre">QuerySet</span></code></a> 实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">queryset</span> <span class="o">=</span> <span class="n">Book</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">title__startswith</span><span class="o">=</span><span class="s1">&#39;M&#39;</span><span class="p">)</span>
<span class="n">get_object_or_404</span><span class="p">(</span><span class="n">queryset</span><span class="p">,</span> <span class="n">pk</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
</pre></div>
</div>
<p>以上例子有点冗长，因为它等同于：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">get_object_or_404</span><span class="p">(</span><span class="n">Book</span><span class="p">,</span> <span class="n">title__startswith</span><span class="o">=</span><span class="s1">&#39;M&#39;</span><span class="p">,</span> <span class="n">pk</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
</pre></div>
</div>
<p>但如果你是从其他地方传递的 <code class="docutils literal notranslate"><span class="pre">queryset</span></code> 变量，那它会很有用。</p>
<p>最后，你也可以使用 <a class="reference internal" href="../db/managers.html#django.db.models.Manager" title="django.db.models.Manager"><code class="xref py py-class docutils literal notranslate"><span class="pre">Manager</span></code></a> 。如果你有自定义管理器( <a class="reference internal" href="../db/managers.html#custom-managers"><span class="std std-ref">custom manager</span></a> )会很有用：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">get_object_or_404</span><span class="p">(</span><span class="n">Book</span><span class="o">.</span><span class="n">dahl_objects</span><span class="p">,</span> <span class="n">title</span><span class="o">=</span><span class="s1">&#39;Matilda&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>你也可以使用关联管理器( <a class="reference internal" href="../../ref/models/relations.html#django.db.models.fields.related.RelatedManager" title="django.db.models.fields.related.RelatedManager"><code class="xref py py-class docutils literal notranslate"><span class="pre">related</span> <span class="pre">managers</span></code></a> )：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">author</span> <span class="o">=</span> <span class="n">Author</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&#39;Roald Dahl&#39;</span><span class="p">)</span>
<span class="n">get_object_or_404</span><span class="p">(</span><span class="n">author</span><span class="o">.</span><span class="n">book_set</span><span class="p">,</span> <span class="n">title</span><span class="o">=</span><span class="s1">&#39;Matilda&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>注意：与 <code class="docutils literal notranslate"><span class="pre">get()</span></code> 一样，如果查询结果有多个对象，那么会引发 <a class="reference internal" href="../../ref/exceptions.html#django.core.exceptions.MultipleObjectsReturned" title="django.core.exceptions.MultipleObjectsReturned"><code class="xref py py-class docutils literal notranslate"><span class="pre">MultipleObjectsReturned</span></code></a> 异常。</p>
</div>
</div>
<div class="section" id="s-get-list-or-404">
<span id="get-list-or-404"></span><h2><code class="docutils literal notranslate"><span class="pre">get_list_or_404()</span></code><a class="headerlink" href="#get-list-or-404" title="永久链接至标题">¶</a></h2>
<dl class="function">
<dt id="django.shortcuts.get_list_or_404">
<code class="descname">get_list_or_404</code>(<em>klass</em>, <em>*args</em>, <em>**kwargs</em>)<a class="headerlink" href="#django.shortcuts.get_list_or_404" title="永久链接至目标">¶</a></dt>
<dd><p>返回给定模型管理器上 <a class="reference internal" href="../../ref/models/querysets.html#django.db.models.query.QuerySet.filter" title="django.db.models.query.QuerySet.filter"><code class="xref py py-meth docutils literal notranslate"><span class="pre">filter()</span></code></a> 转换为列表的结果，如果结果列表为空，则引发 <a class="reference internal" href="views.html#django.http.Http404" title="django.http.Http404"><code class="xref py py-class docutils literal notranslate"><span class="pre">Http404</span></code></a>。</p>
</dd></dl>

<div class="section" id="s-id3">
<span id="id3"></span><h3>必选参数<a class="headerlink" href="#id3" title="永久链接至标题">¶</a></h3>
<dl class="docutils">
<dt><code class="docutils literal notranslate"><span class="pre">klass</span></code></dt>
<dd>从中获取列表的 <a class="reference internal" href="../../ref/models/instances.html#django.db.models.Model" title="django.db.models.Model"><code class="xref py py-class docutils literal notranslate"><span class="pre">Model</span></code></a> ，<a class="reference internal" href="../db/managers.html#django.db.models.Manager" title="django.db.models.Manager"><code class="xref py py-class docutils literal notranslate"><span class="pre">Manager</span></code></a> 或 <a class="reference internal" href="../../ref/models/querysets.html#django.db.models.query.QuerySet" title="django.db.models.query.QuerySet"><code class="xref py py-class docutils literal notranslate"><span class="pre">QuerySet</span></code></a> 实例。</dd>
<dt><code class="docutils literal notranslate"><span class="pre">**kwargs</span></code></dt>
<dd>查询参数，应采用 <code class="docutils literal notranslate"><span class="pre">get()</span></code> 和 <code class="docutils literal notranslate"><span class="pre">filter()</span></code> 接受的格式。</dd>
</dl>
</div>
<div class="section" id="s-id4">
<span id="id4"></span><h3>例如<a class="headerlink" href="#id4" title="永久链接至标题">¶</a></h3>
<p>下面的例子展示从 <code class="docutils literal notranslate"><span class="pre">MyModel</span></code> 中获取所有 published=True 的对象：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.shortcuts</span> <span class="kn">import</span> <span class="n">get_list_or_404</span>

<span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="n">my_objects</span> <span class="o">=</span> <span class="n">get_list_or_404</span><span class="p">(</span><span class="n">MyModel</span><span class="p">,</span> <span class="n">published</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</pre></div>
</div>
<p>此示例相当于：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.http</span> <span class="kn">import</span> <span class="n">Http404</span>

<span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="n">my_objects</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">MyModel</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">published</span><span class="o">=</span><span class="kc">True</span><span class="p">))</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">my_objects</span><span class="p">:</span>
        <span class="k">raise</span> <span class="n">Http404</span><span class="p">(</span><span class="s2">&quot;No MyModel matches the given query.&quot;</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Django 便捷函数</a><ul>
<li><a class="reference internal" href="#render"><code class="docutils literal notranslate"><span class="pre">render()</span></code></a><ul>
<li><a class="reference internal" href="#required-arguments">必选参数</a></li>
<li><a class="reference internal" href="#optional-arguments">可选参数</a></li>
<li><a class="reference internal" href="#example">例如</a></li>
</ul>
</li>
<li><a class="reference internal" href="#redirect"><code class="docutils literal notranslate"><span class="pre">redirect()</span></code></a><ul>
<li><a class="reference internal" href="#examples">示例</a></li>
</ul>
</li>
<li><a class="reference internal" href="#get-object-or-404"><code class="docutils literal notranslate"><span class="pre">get_object_or_404()</span></code></a><ul>
<li><a class="reference internal" href="#id1">必选参数</a></li>
<li><a class="reference internal" href="#id2">例如</a></li>
</ul>
</li>
<li><a class="reference internal" href="#get-list-or-404"><code class="docutils literal notranslate"><span class="pre">get_list_or_404()</span></code></a><ul>
<li><a class="reference internal" href="#id3">必选参数</a></li>
<li><a class="reference internal" href="#id4">例如</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="file-uploads.html"
                        title="上一章">文件上传</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="generic-views.html"
                        title="下一章">通用视图</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../../_sources/topics/http/shortcuts.txt"
            rel="nofollow">显示源代码</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>快速搜索</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="转向" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">12月 07, 2021</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="file-uploads.html" title="文件上传">previous</a>
     |
    <a href="../index.html" title="使用 Django" accesskey="U">up</a>
   |
    <a href="generic-views.html" title="通用视图">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>